/**
 * 充值
 * @author ：szy
 */
define(function (require, exports, module) {


    var UI = require("js/UI");
    var $homeCnt = $("#home .cnt");


    var reg_num = /^[1-9]+(\d)*$/;


    var $homeMenu = $homeCnt.find(".homeMenu");
    var $bank = $homeCnt.find(".payWrap .bank"),
        $form = $homeCnt.find(".payWrap form"),
        $payTo=$homeCnt.find(".payTo");

    var $selectBankCard=$homeCnt.find(".payWrap,.homeMenu")

    var $pagMenu = $homeMenu.find(".list a"),$pay=$form.find("input[name='payWay']");

    //选择支付方式
    $pagMenu.unbind("click.sel").bind("click.sel", function () {
        var $this = $(this), _index = $this.index();
        $pagMenu.removeClass("curr");
        $this.addClass("curr");
        $bank.hide().eq(_index).fadeIn();
        $bankCard.removeClass("curr");
        $pay.val("");
        return false
    })
    // 选择银行卡 或支付接口
    var $bankCard = $bank.find("a").unbind("click.selected").bind("click.selected", function () {
        var $this = $(this), _id = $this.attr("id")
        $bankCard.removeClass("curr");
        $this.addClass("curr");
        $pay.val(_id);
        return false
    })
    //提交
    $form.find("button[type='submit']").unbind("click.sub").bind("click.sub", function () {
        var _val = $.trim($form.find("input[type=hidden]").val()),
            $money = $form.find("span input"),
            _money = $money.val();
        if (!_val) {
            UI.tooltip("error", "请选择支付方式");
            return false
        }
        if (!_money) {
            UI.tooltip("error", "请输入充值金额");
            $money.focus();
            return false
        } else {
            if (!_money.match(reg_num)) {
                UI.tooltip("error", "请输入正整数");
                $money.focus();
                return false
            }
        }
        $payTo.find("a img").attr("src",$bankCard.filter(".curr").find("img").attr("src"));
        $payTo.find("div em").text(_money);
        $selectBankCard.hide();
        $payTo.show();



        return false
    })
    //返回上一级
    $payTo.find(".btnWrap a").unbind("click.prev").bind("click.prev", function () {
        $selectBankCard.show();
        $payTo.hide();
        return false;
    })

    $payTo.find("[type='submit']").unbind("click.sub").bind("click.sub", function () {
        var payTipId=$("#payTipId").val();

        var $payDialogTip=$(template("home/user/payDialogTip",{payTipId:payTipId}));
        UI.dialog({
            $tpl:$payDialogTip
        })
    })

})